var url = require('url');
var template = require('templateConfig');
var main = require('main');
var form = require('form');
var module= require('module');

//模板配置
var tplArr = [];
var addressTpl =require('raw!../template/address.txt'); 
tplArr['address'] = template.compile(addressTpl.replace(/^\s*|\s*$/g, "")); 
 
var xId = main.GetQueryString('id');//获取任务id

function Index(){
    _this = this;
    this.main = function(){
        main.event(this,'tap','i-click'); 
        main.event(this,'tap','z-click'); 
        this.init();
    }  
    
    //
    this.getContent= function() {
    	var dataAddress= {
    		tType: "address",
    		name: "addr[]",
    		dataList: [
      //       {
    		// 	address: "上海徐汇区斜土路2899号光启文化广场1003室",
    		// 	name: "赵敏",
    		// 	phone: "13524667719",
    		// 	checked: true,
    		// 	value: 1,
    		// 	btnName: "编辑",

    		// },{
    		// 	address: "上海徐汇区斜土路2899号光启文化广场1003室",
    		// 	name: "赵敏",
    		// 	phone: "13524667719",
    		// 	value: 2,
    		// 	btnName: "编辑",
    		// }
            ],  		
    	};
    	//超链接列表
		var dataArrlist= { 
		    dataList: [{
		        name: "新增收货地址",
		        link: url.newAddress+'?id='+xId,
		    },{
		        name: "从微信选择地址",
		        link: ""
		    }]
		};
        main.post(url.postAddressList,{},function(res){
            if(res.code==1){
                var ret=res.data;
                $.each(ret, function(i, v){ 
                    // console.log(i);
                    var sin={
                        addressId:i,
                        address: v.province_name+v.city_name+v.district_name+v.address,
                        name: v.name,
                        phone: v.mobile,
                        checked: true,
                        value: 1,
                        btnName: "编辑",
                        edit:"editAddress", 
                        zClick:'z-click="check_fun"'
                    }
                    if(v.is_default==1){
                        sin.checked=true;
                    }else{
                        sin.checked=false;
                    }
                    // console.log(sin);
                    dataAddress.dataList.push(sin);
                })
                var data= {
                    tType: "content",
                    address: module.getModule(dataAddress, "checklist"),
                    addAddress: module.getModule(dataArrlist, "arrlist"),
                    btnName: "选择",
                    link: "javascript:;",
                    click:"back_btn",
                }
                $(".content").html(tplArr['address'](data));
            }else{
                alert(res.data);
            } 
        })
    } 
    //编辑按钮
    this.editAddress=function(ths){
        var id=$(ths).parent().attr("address-id");
        window.location.href=url.newAddress+'?address_id='+id+'&id='+xId;
    }
    //多选框
    this.check_fun=function(ths){
        // $("input").removeAttr("checked");
        // $(ths).find("input").attr("checked","checked");
        this.getId=$(ths).parent().attr("address-id");
        console.log(this.getId);
    }
    //选择按钮
    this.back_btn=function(){
        window.location.href=url.orderConfirm+'?address_id='+_this.getId+'&id='+xId;
    }
    this.getFooter= function () {        
    	var data= { 
    		number: "12345", 
            active: "2",
    		dataList: [{
    			link: url.Index,
    			icon: "ui-icon-home",
    			name: "首页"
    		},{
    			link: url.shopCar,
    			icon: "ui-icon-cart",
    			name: "购物车"
    		},{
    			link: url.Center,
    			icon: "ui-icon-personal",
    			name: "个人中心"
    		}] 
    	} 
    	$(".wrap").append(module.getModule(data, "footer")); 
    }
  

    this.init= function(){  
        this.getContent();   
        this.getFooter(); 
        this.getId;    
    }
    return this.main();
};

var index = new Index();